package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.mapper.common;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.menu.Menu;
import com.gwtext.client.widgets.menu.MenuItem;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.data.EntityData;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.treepanel.StkosPropertyTreePanel;

/**
 * Stkos Mapper Property Tree Panel
 * 
 * @author LWS
 * 
 */
public class StkosMapperPropertyTreePanel extends StkosPropertyTreePanel {

	private Project accordingProject;

	public StkosMapperPropertyTreePanel(Project project) {
		this(project, null);
	}

	public StkosMapperPropertyTreePanel(Project project,
			Project accordingProject) {
		super(project);
		this.accordingProject = accordingProject;
	}

	@Override
	protected TreeNode createTreeNode(EntityData entityData) {
		TreeNode node = super.createTreeNode(entityData);
		if (node == null) {
			return node;
		}

		if (accordingProject != null) {
			addAnnotationMappingNoNodeMark(node, entityData, accordingProject
					.getProjectId().getId());
		}

		return node;
	}

	@Override
	protected void onContextMenu(final TreeNode node, final EventObject e) {
		super.onContextMenu(node, e);

		Menu contextMenu = new Menu();

		if (node != null && getProject() != null) {
			MenuItem menuModifyMapperNumber = new StkosMapperModifyMapperNumberMenuItem(
					this, getProject(), accordingProject, node);
			contextMenu.addItem(menuModifyMapperNumber);
			
			MenuItem menuDeleteMapperNumber = new StkosMapperDeleteMapperNumberMenuItem(
					this, getProject(), accordingProject, node);
			contextMenu.addItem(menuDeleteMapperNumber);
		}

		contextMenu.showAt(e.getXY()[0] + 5, e.getXY()[1] + 5);
	}

	public void setAccordingProject(Project accordingProject) {
		this.accordingProject = accordingProject;
		onRefresh();
	}
}
